package com.xpffly.design.memento.simple;

/**
 * @Author: 午夜学徒
 * @Date: 2022/4/4 22:14
 * @Description：精简-备忘录模式--发起人---实现Cloneable接口，以实现自动备份和恢复
 */
public class Originator implements Cloneable {
    private Originator bakUp;
    /**
     * 内部状态
     */
    private String state = "";

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    /**
     * 创建一个备忘录
     */
    public void createMemento() {
        this.bakUp = this.clone();
    }

    /**
     * 恢复一个备忘录
     */
    public void recoverMemento() {
        this.setState(this.bakUp.getState());
    }

    /**
     * 重写clone方法
     *
     * @return
     */
    @Override
    protected Originator clone() {
        try {
            return (Originator) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
